1 /*
2 * Copyright (C) 2007 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.common.testing;
18
19 import com.google.common.annotations.Beta;
20 import com.google.common.annotations.GwtCompatible;
21
22 import junit.framework.Assert;
23 import junit.framework.AssertionFailedError;
24
25 /**
26 * Tests serialization and deserialization of an object, optionally asserting
27 * that the resulting object is equal to the original.
28 *
29 * <p><b>GWT warning:</b> Under GWT, both methods simply returns their input,
30 * as proper GWT serialization tests require more setup. This no-op behavior
31 * allows test authors to intersperse {@code SerializableTester} calls with
32 * other, GWT-compatible tests.
33 *
34 *
35 * @author Mike Bostock
36 * @since 10.0
37 */
38 @Beta
39 @GwtCompatible // but no-op!
40 public final class SerializableTester {
41 private SerializableTester() {}
42
43 /**
44 * Serializes and deserializes the specified object.
45 *
46 * <p><b>GWT warning:</b> Under GWT, this method simply returns its input, as
47 * proper GWT serialization tests require more setup. This no-op behavior
48 * allows test authors to intersperse {@code SerializableTester} calls with
49 * other, GWT-compatible tests.
50 *
51 * <p>Note that the specified object may not be known by the compiler to be a
52 * {@link java.io.Serializable} instance, and is thus declared an
53 * {@code Object}. For example, it might be declared as a {@code List}.
54 *
55 * @return the re-serialized object
56 * @throws RuntimeException if the specified object was not successfully
57 * serialized or deserialized
58 */
59 @SuppressWarnings("unchecked")
60 public static <T> T reserialize(T object) {
61 return Platform.reserialize(object);
62 }
63
64 /**
65 * Serializes and deserializes the specified object and verifies that the
66 * re-serialized object is equal to the provided object, that the hashcodes
67 * are identical, and that the class of the re-serialized object is identical
68 * to that of the original.
69 *
70 * <p><b>GWT warning:</b> Under GWT, this method simply returns its input, as
71 * proper GWT serialization tests require more setup. This no-op behavior
72 * allows test authors to intersperse {@code SerializableTester} calls with
73 * other, GWT-compatible tests.
74 *
75 * <p>Note that the specified object may not be known by the compiler to be a
76 * {@link java.io.Serializable} instance, and is thus declared an
77 * {@code Object}. For example, it might be declared as a {@code List}.
78 *
79 * <p>Note also that serialization is not in general required to return an
80 * object that is {@linkplain Object#equals equal} to the original, nor is it
81 * required to return even an object of the same class. For example, if
82 * sublists of {@code MyList} instances were serializable, those sublists
83 * might implement a private {@code MySubList} type but serialize as a plain
84 * {@code MyList} to save space. So long as {@code MyList} has all the public
85 * supertypes of {@code MySubList}, this is safe. For these cases, for which
86 * {@code reserializeAndAssert} is too strict, use {@link #reserialize}.
87 *
88 * @return the re-serialized object
89 * @throws RuntimeException if the specified object was not successfully
90 * serialized or deserialized
91 * @throws AssertionFailedError if the re-serialized object is not equal to
92 * the original object, or if the hashcodes are different.
93 */
94 public static <T> T reserializeAndAssert(T object) {
95 T copy = reserialize(object);
96 new EqualsTester()
97 .addEqualityGroup(object, copy)
98 .testEquals();
99 Assert.assertEquals(object.getClass(), copy.getClass());
100 return copy;
101 }
102 }